using System;
using System.Collections.Generic;
using System.Text;
using Jbe.TestSuite.Infrastructure.Interface;
using Jbe.TestSuite.RTFEditor.Constants;
using Microsoft.Practices.CompositeUI.SmartParts;
using System.IO;
using Jbe.TestSuite.Infrastructure.Interface.Services;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.Practices.CompositeUI;
using Jbe.CABExtension.WinForms;
using Jbe.TestSuite.RTFEditor.Properties;
using Jbe.CABExtension.WinForms.Workspaces;
using Jbe.CABExtension.SmartPartInfos;

namespace Jbe.TestSuite.RTFEditor
{
    internal class DocumentController : WorkItemController, IDocument
    {
        public event EventHandler DocumentActivated;
        public event EventHandler DocumentDeactivated;
        public event EventHandler Disposed;

        private IEditorView editorView;
        private IconSmartPartInfo smartPartInfo;
        private string fileName = null;
        private IDocumentType documentType;

        public string DocumentTitle
        {
            get 
            {
                if (fileName == null) { return "New Document"; }

                return Path.GetFileName(fileName);
            }
        }

        private string SmartPartTitle
        {
            get { return DocumentTitle + (editorView.Modified ? "*" : ""); }
        }

        public IDocumentType DocumentType
        {
            get { return documentType; }
            set { documentType = value; }
        }

        public string FileName
        {
            get { return fileName; }
        }
        
        public override void Run()
        {
            WorkItem.Activated += new EventHandler(WorkItemActivated);
            WorkItem.Deactivated += new EventHandler(WorkItemDeactivated);

            editorView = WorkItem.SmartParts.AddNew<EditorView>();
            smartPartInfo = new IconSmartPartInfo(SmartPartTitle, SmartPartTitle);
            smartPartInfo.Icon = Resources.TextDoc;
            WorkItem.Workspaces[WorkspaceNames.DockPanelWorkspace].Show(editorView, smartPartInfo);
            WorkItem.Workspaces[WorkspaceNames.DockPanelWorkspace].SmartPartClosing += new EventHandler<WorkspaceCancelEventArgs>(WorkspaceSmartPartClosing);
            
            editorView.Disposed += new EventHandler(EditorViewDisposed);
            editorView.ModifiedChanged += new EventHandler(EditorViewModifiedChanged);
            editorView.Modified = true;
        }

        private void WorkspaceSmartPartClosing(object sender, WorkspaceCancelEventArgs e)
        {
            if (e.SmartPart == editorView)
            {
                if (editorView.Modified)
                {
                    DialogResult result = MessageBox.Show("Do you want to save the changes to " + DocumentTitle + "?",
                        Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
                    switch (result)
                    {
                        case DialogResult.Yes:
                            WorkItem.Commands[CommandNames.Save].Execute();
                            break;
                        case DialogResult.No:
                            break;
                        case DialogResult.Cancel:
                            e.Cancel = true;
                            break;
                        default:
                            Debug.Assert(false, "The default case is not allowed!");
                            break;
                    }
                }
            }
        }

        public void Open(string fileName)
        {
            editorView.Open(fileName);
            this.fileName = fileName;
            editorView.Modified = false;
            UpdateUI();
        }

        public void Save(string fileName)
        {
            editorView.Save(fileName);
            this.fileName = fileName;
            editorView.Modified = false;
            UpdateUI();
        }

        private void UpdateUI()
        {
            smartPartInfo.Title = SmartPartTitle;
            smartPartInfo.Description = SmartPartTitle;
            WorkItem.Workspaces[WorkspaceNames.DockPanelWorkspace].ApplySmartPartInfo(editorView, smartPartInfo);
        }

        #region Command and Event handler

        private void WorkItemActivated(object sender, EventArgs e)
        {
            if (DocumentActivated != null) { DocumentActivated(this, e); }
        }

        private void WorkItemDeactivated(object sender, EventArgs e)
        {
            if (DocumentDeactivated != null) { DocumentDeactivated(this, e); }
        }

        private void EditorViewDisposed(object sender, EventArgs e)
        {
            WorkItem.Terminate();
            if (Disposed != null) { Disposed(this, e); }
        }

        private void EditorViewModifiedChanged(object sender, EventArgs e)
        {
            UpdateUI();
        }

        #endregion
    }
}
